import * as THREE from '../../../node_modules/three/src/Three.js'
import gsap from 'gsap'
class Geometry {
  constructor() {
    this.raycaster = new THREE.Raycaster()
    this.mouse = new THREE.Vector2()
    this.pickedObject = null
    this.pickedObjectSavedColor = 0
  }
  // 盒子
  geometry(obj, obj1) {
    const sanMesh = new THREE.Mesh(
      new THREE.BoxGeometry(...obj),
      new THREE.MeshPhongMaterial({
        flatShading: true,
        ...obj1
      })
    )
    return sanMesh
  }

  // 盒子
  cylinder(obj, obj1) {
    const sanMesh = new THREE.Mesh(
      new THREE.CylinderGeometry(...obj),
      new THREE.MeshPhongMaterial({
        flatShading: true,
        ...obj1
      })
    )
    return sanMesh
  }

  onDocumentMouseDown(event, scene, camera) {
    //鼠标事件开始
    event.preventDefault()
    this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1
    this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1
    // 通过摄像机和鼠标位置更新射线
    this.raycaster.setFromCamera(this.mouse, camera)

    // 计算物体和射线的焦点
    var intersects = this.raycaster.intersectObjects(scene.children, true) //射线穿过物体，自动由近到远排序
    //第二种
    if (intersects.length > 0) {
      //有物体的时候
      const itemG = intersects[0].object

      return itemG
    }
  } //mousedown鼠标事件结束
  // 相机移动动画
  animateCamera = (initConfig, newP, newT, time = 5) => {
    gsap.to(initConfig.camera.position, {
      duration: time,
      x: newP.x,
      y: newP.y,
      z: newP.z - 50
    })

    //设置相机方向

    gsap.to(initConfig.camera.lookAt, {
      duration: time,
      x: newP.x,
      y: newP.y,
      z: newP.z - 20
      // onComplete: callBack()
    })
    // ...
  }
}

export default Geometry
